/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ProcessoRelatorioClienteGUI.java
 *
 * Created on 06/03/2010, 17:07:03
 */

package gui;

import facade.AdvogadoFacade;
import java.util.List;
import javax.swing.table.DefaultTableModel;
import modelo.Cliente;
import modelo.Processo;

/**
 *
 * @author andrehadm
 */
public class ProcessoRelatorioClienteGUI extends javax.swing.JPanel {

    AdvogadoFacade advogadoFacade;

    /** Creates new form ProcessoRelatorioClienteGUI */
    public ProcessoRelatorioClienteGUI(AdvogadoFacade advogadoFacade) {
        this.advogadoFacade = advogadoFacade;
        initComponents();
        carregaClientes();
    }

    public void carregaProcessos() {
        System.out.println("");
        System.out.println("Carregando processos");
        List <Processo> processo = advogadoFacade.getTodosProcessos();

        Object[][] processoTable = new Object[processo.size()][3]; // Define uma matriz bidimensional para armazenar os valores
        Object[] titulo = new Object[3]; // Define uma matriz unidimensional para armazenar os titulos


        jTable1.removeAll();
        int processoContador = 0;

        Long clienteID = null;
        if (jComboBox_Clientes.getItemCount() > 0) {
            Cliente cliente = (Cliente) jComboBox_Clientes.getSelectedItem();
            clienteID = cliente.getId();
        }

        for (Processo processoFor : advogadoFacade.pesquisarProcessoPorCliente(clienteID)) {
            processoTable[processoContador][0] = processoFor.getId().toString();
            processoTable[processoContador][1] = processoFor.getNome();
            String acao = "";
            if (processoFor.getIdTipoDaAcao() != null){
                acao = advogadoFacade.pesquisarAcaoPorId(processoFor.getIdTipoDaAcao()).getNome();
            }
            processoTable[processoContador][2] = acao;
            processoContador++;
            System.out.println("Processo: "+ processoFor.getId() + " - " + processoFor.getNome());
        }
        System.out.println("Processos carregados.");

        // titulos
        titulo[0] = "Id";
        titulo[1] = "Processo";
        titulo[2] = "Tipo de Ação"; // criando objeto modelo

        DefaultTableModel modelo = new DefaultTableModel(processoTable, titulo);

        // definindo o modelo da tabela
        jTable1.setModel(modelo);
    }

    private void carregaClientes () {
        jComboBox_Clientes.removeAllItems();
        System.out.println("");
        System.out.println("Carregando clientes");
        for (Cliente cliente : advogadoFacade.getTodosClientes()) {
            jComboBox_Clientes.addItem(cliente);
            System.out.println("Cliente: "+ cliente.getId() + " - " + cliente.getNome());
        }
        System.out.println("Tamanho da lista de clientes: " + jComboBox_Clientes.getItemCount());
        System.out.println("Clientes carregados.");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jComboBox_Clientes = new javax.swing.JComboBox();
        jButton_Carregar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        jButton_Carregar.setText("Carregar");
        jButton_Carregar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton_CarregarMouseClicked(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Id", "Processo", "Tipo de Ação"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jComboBox_Clientes, 0, 330, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton_Carregar))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBox_Clientes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton_Carregar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 269, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton_CarregarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton_CarregarMouseClicked
        carregaProcessos();
    }//GEN-LAST:event_jButton_CarregarMouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton_Carregar;
    private javax.swing.JComboBox jComboBox_Clientes;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables

}
